package com.moxiu.browser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.moxiu.browser.provider.BrowserContract;
import com.moxiu.launcher.R;
import com.moxiu.launcher.R.drawable;

public class BookMarksAdapter extends BaseAdapter{
	public static final String TAG = "BookMarksAdapter"; 
	
	private List<Map<String, String>> value;
	private List<Map<String, String>> selectItemList;    //选中的选项的item的数据集合。
	private List<Integer> selectPositonList;             //选中选项的下标集合。
	private List<byte[]> byteBmp;
	private Context ctx;
	private boolean isNight;
	
	private static boolean isEdit;
	private static boolean isShow;
	private ViewHolder viewHolder;
	private Drawable deleteCbDrawable;
	private Drawable selecteDrawable;
	private int pid28;
	
	public BookMarksAdapter(Context ctx, List<Map<String, String>> value, List<byte[]> byteBmp, boolean isNight){
		this.selectItemList = new ArrayList<Map<String,String>>();
		this.selectPositonList = new ArrayList<Integer>();
		this.ctx = ctx;
		this.value = value;
		this.byteBmp = byteBmp;
		this.isNight = isNight;
		this.deleteCbDrawable = ctx.getResources().getDrawable(R.drawable.br__edit_btn);
		this.selecteDrawable = ctx.getResources().getDrawable(R.drawable.br_unselected_edit);
		this.pid28=dip2px(ctx, 28);
		if (isNight) {
			deleteCbDrawable.setColorFilter(Color.WHITE, Mode.SRC_IN);
			selecteDrawable.setColorFilter(Color.WHITE, Mode.SRC_IN);
		}else {
			deleteCbDrawable.setColorFilter(Color.BLACK, Mode.SRC_IN);
			selecteDrawable.setColorFilter(Color.BLACK, Mode.SRC_IN);
		}
	}

	public boolean isEdit() {
		return isEdit;
	}

	public void setEdit(boolean isEdit) {
		selectItemList.clear();
		this.isEdit = isEdit;
	}
	
	@Override
	public int getCount() {
		if (value == null) {
			return 0;
		}
		return value.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (value == null) {
			return null;
		}
		if (convertView == null) {
			convertView = LayoutInflater.from(ctx).inflate(R.layout.br_history_item, null);
			viewHolder = new ViewHolder();
			viewHolder.faviconIv = (ImageView) convertView.findViewById(R.id.favicon);
			viewHolder.titleTv = (TextView) convertView.findViewById(R.id.title);
			viewHolder.urlTv = (TextView) convertView.findViewById(R.id.url);
			viewHolder.deleteCb = (CheckBox) convertView.findViewById(R.id.star);
			viewHolder.editIv = (ImageView) convertView.findViewById(R.id.edit_item_iv);
			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		if (isNight) {
			viewHolder.titleTv.setTextColor(ctx.getResources().getColor(R.color.br_night_text));
			viewHolder.urlTv.setTextColor(ctx.getResources().getColor(R.color.br_night_text));
			convertView.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.br_home_night_press));
			convertView.setPadding(pid28, 0, pid28, 0);
		}else {
			viewHolder.titleTv.setTextColor(ctx.getResources().getColor(R.color.moxiu_title_text));
			viewHolder.urlTv.setTextColor(ctx.getResources().getColor(R.color.feedback_text_hint));
			convertView.setBackgroundDrawable(ctx.getResources().getDrawable(R.drawable.b_flow_new_bg));
			convertView.setPadding(pid28, 0, pid28, 0);
		}
		viewHolder.deleteCb.setButtonDrawable(deleteCbDrawable);
		final int nowPosition = position;
		viewHolder.deleteCb.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent in = new Intent();
				in.setClass(ctx, EditBookMarkHistoryActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("bookmarkId", value.get(nowPosition).get("id"));
				bundle.putString("bookmarkName", value.get(nowPosition).get("title"));
				bundle.putString("bookmarkUrl", value.get(nowPosition).get("url"));
				in.putExtras(bundle);
				ctx.startActivity(in);
				notifyDataSetChanged();
			}
		});
		
		if (isEdit) {
			viewHolder.editIv.setVisibility(View.VISIBLE);
		}else {
			viewHolder.editIv.setVisibility(View.GONE);
		}
		
		selectItemShow(position);
		
		if (value.size() > 0) {
			showDeleteIcon(isShow);
		}
		
		if (byteBmp.get(position) == null) {
			viewHolder.faviconIv.setImageResource(R.drawable.br_bookmarks_history_no_image);
		}else {
			Bitmap bmp = BitmapFactory.decodeByteArray(byteBmp.get(position), 0, byteBmp.get(position).length);
			viewHolder.faviconIv.setImageBitmap(bmp);
			
		}
		//bmp.recycle();
		viewHolder.titleTv.setText(value.get(position).get("title"));
		viewHolder.urlTv.setText(value.get(position).get("url"));
		
		return convertView;
	}
	
	/*
	 * 显示已经选中的item
	 */
	private void selectItemShow(int position) {
		if (selectPositonList.contains(position)) {
			viewHolder.editIv.setImageResource(R.drawable.br_selected_edit);
		}else {
//			viewHolder.editIv.setImageResource(R.drawable.br_unselected_edit);
			viewHolder.editIv.setImageDrawable(selecteDrawable);
		}
	}

	private static class ViewHolder{
	    TextView titleTv;
		TextView urlTv;
		ImageView faviconIv;
		CheckBox deleteCb;
		ImageView editIv;
	}
	
	/*
	 * 设置已选择的item函数
	 */
	public void setSelectPosition(List<Integer> selectPosition){
		this.selectPositonList = selectPosition;
		notifyDataSetChanged();
	}
	
	public void showDeleteIcon(boolean isShow){
		if (isShow) {
			viewHolder.deleteCb.setVisibility(View.VISIBLE);
		}else {
			viewHolder.deleteCb.setVisibility(View.GONE);
		}
	}
	
	public void editIsShow(boolean state){
		this.isShow = state;
	}
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}
}
